package com.hj.ct.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.hj.ct.App;
import com.hj.ct.R;
import com.hj.ct.db.dao.DeviceDao;
import com.hj.ct.db.dao.SetAlarmDao;
import com.hj.ct.logic.BaseLogic.OnHandlerResponse;
import com.hj.ct.logic.LogicActions;
import com.hj.ct.model.SetAlarm;
import com.hj.ct.parse.Device;
import com.hj.ct.util.SetAlarmUtil;
import com.hj.ct.view.ListPopupWindow;

public class SettingFragment extends BaseFragment implements OnHandlerResponse,
		OnClickListener {
	private TextView tvDeviceFilter;
	private DeviceDao deviceDao;
	private SetAlarmDao alarmDao;
	private View viewTitle;
	private List<Device> devices;
	private ListPopupWindow mPopupWindow;
	private String deviceId;
	private String deviceName;
	private View scrollContent;

	private TextView dev_id;
	private TextView dust_alarm_type;
	private TextView dust_hi_day;
	private TextView dust_hi_night;
	private TextView dust_delay;
	private TextView noise_alarm_type;
	private TextView noise_hi_day;
	private TextView noise_hi_night;
	private TextView noise_hi_delay;
	private TextView day_time_start;
	private TextView night_time_start;

	private HashMap<String, SetAlarm> alarms;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_setting, null);
		view.findViewById(R.id.iv_back).setOnClickListener(
				new OnClickListener() {
					public void onClick(View v) {
						getActivity().finish();
					}
				});
		scrollContent = view.findViewById(R.id.scroll_content);

		dev_id = (TextView) view.findViewById(R.id.dev_id);
		dust_alarm_type = (TextView) view.findViewById(R.id.dust_alarm_type);
		dust_hi_day = (TextView) view.findViewById(R.id.dust_hi_day);
		dust_hi_night = (TextView) view.findViewById(R.id.dust_hi_night);
		dust_delay = (TextView) view.findViewById(R.id.dust_delay);
		noise_alarm_type = (TextView) view.findViewById(R.id.noise_alarm_type);
		noise_hi_day = (TextView) view.findViewById(R.id.noise_hi_day);
		noise_hi_night = (TextView) view.findViewById(R.id.noise_hi_night);
		noise_hi_delay = (TextView) view.findViewById(R.id.noise_hi_delay);
		day_time_start = (TextView) view.findViewById(R.id.day_time_start);
		night_time_start = (TextView) view.findViewById(R.id.night_time_start);

		deviceDao = new DeviceDao(App.getContext());
		alarmDao = new SetAlarmDao(App.getContext());
		tvDeviceFilter = (TextView) view.findViewById(R.id.tv_device_filter);
		tvDeviceFilter.setOnClickListener(this);
		viewTitle = view.findViewById(R.id.viewTitle);
		devices = deviceDao.getDevices();
		alarms = alarmDao.selectAlarms();
		if (devices != null && devices.size() > 0) {
			deviceId = devices.get(0).getDeviceID();
			deviceName = devices.get(0).getDeviceName();
			tvDeviceFilter.setText(deviceName);
			initData(deviceId);
		} else {
			tvDeviceFilter.setText("暂无设备");
			scrollContent.setVisibility(View.GONE);
		}
		return view;
	}

	private void initData(String deviceId) {
		SetAlarm alarm = alarms.get(deviceId);
		if (alarm == null)
			return;
		dev_id.setText(alarm.getDeviceID());
		dust_alarm_type.setText(SetAlarmUtil.getDustType(alarm.getDustAlarmType()));
		dust_hi_day.setText(alarm.getDustHi_Day() + "	mg/m³");
		dust_hi_night.setText(alarm.getDustHi_Night() + "	mg/m³");
		dust_delay.setText(alarm.getDustDelay()+"	分钟");
		noise_alarm_type.setText(SetAlarmUtil.getNoiseType(alarm.getNoiseAlarmType()));
		noise_hi_day.setText(alarm.getNoiseHi_Day() + "	db");
		noise_hi_night.setText(alarm.getNoiseHi_Night() + "	db");
		noise_hi_delay.setText(alarm.getNoiseDelay()+"	分钟");
		day_time_start.setText(alarm.getDayTimeStart());
		night_time_start.setText(alarm.getNightTimeStart());
	}

	public void onClick(View v) {
		if (v == tvDeviceFilter) {
			initDeviceFilterPopupWindow();
		}
	}

	private void initDeviceFilterPopupWindow() {
		if (mPopupWindow == null) {
			mPopupWindow = new ListPopupWindow(getActivity());
		}
		if (devices == null) {
			devices = deviceDao.getDevices();
		}
		List<String> data = new ArrayList<String>();
		for (int i = 0; i < devices.size(); i++) {
			data.add(devices.get(i).getDeviceName());
		}
		mPopupWindow.setData(data);
		mPopupWindow.showAsDropDown(viewTitle);
		mPopupWindow.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				tvDeviceFilter.setText(mPopupWindow.getItem(position));
				mPopupWindow.dismiss();
				deviceId = devices.get(position).getDeviceID();
				deviceName = devices.get(position).getDeviceName();
				tvDeviceFilter.setText(deviceName);
				initData(deviceId);
			}
		});
	}

	public void handlerUI(int aciton, int code, Object... obj) {
		dimissDialog();
		if (LogicActions.SET_SUCCESS == aciton) {
			toast("设置成功！");
		} else if (LogicActions.SET_SUCCESS == aciton) {
			toast("设置失败");
		}
	}

}
